package com.wtb.javatool.action;

import com.fy.wetoband.tool.Tool;
import com.fy.wetoband.utils.RequestUtil;

import com.wtb.javatool.common.Result;
import com.wtb.javatool.entity.Dish;
import com.wtb.javatool.entity.Table;
import com.wtb.javatool.service.IDishService;
import com.wtb.javatool.service.ITableService;
import com.wtb.javatool.vo.DishDto;
import com.wtb.javatool.vo.TableDto;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Scope;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;

@Api(value = "javatool", description = "java工具示例")
@Scope(SCOPE_PROTOTYPE) //务必使用多实例模式
@ComponentScan("com.wtb.javatool")
public class JavaToolDemoAction extends Tool {

    @Autowired
    private IDishService dishService;

    @Autowired
    private ITableService tableService;

    @Action
    @ApiOperation(value = "添加菜品", httpMethod = "POST", notes = "添加菜品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", dataType = "string", value = "菜品名称", required = true),
            @ApiImplicitParam(name = "type", dataType = "String", value = "菜品类别", required = true),
            @ApiImplicitParam(name = "price", dataType = "string", value = "菜品价格", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "desc", dataType = "string", value = "菜品描述", required = false),
            @ApiImplicitParam(name = "status", dataType = "int", value = "菜品状态", required = false,defaultValue = "0"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加菜品成功", response = Result.class)
    })
    public Result<String> saveDish(HttpServletRequest request, HttpServletResponse response) {

        String name = RequestUtil.getStringParameter(request, "name");
        String type = RequestUtil.getStringParameter(request,"type");
        String price = RequestUtil.getStringParameter(request, "price");
        String desc = RequestUtil.getStringParameter(request, "desc");
        int status = RequestUtil.getIntegerParameter(request,"status");

        Dish dish = new Dish(name, type,new BigDecimal(price), desc,status);
        System.out.println(type);
        final Integer save = dishService.save(dish);
        if(save <= 0){
            System.out.println(status);
            return Result.error("添加菜品失败");
        }

        return Result.success("添加菜品成功");
    }

    @Action
    public Result<String> helloworld(HttpServletRequest request, HttpServletResponse response) {
        return Result.success("广州城市理工你好");
    }

    @Action
    @ApiOperation(value = "修改菜品", httpMethod = "POST", notes = "修改菜品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Id", dataType = "string", value = "菜品ID", required = true),
            @ApiImplicitParam(name = "name", dataType = "string", value = "菜品名称", required = true),
            @ApiImplicitParam(name = "type", dataType = "String", value = "菜品类别", required = false),
            @ApiImplicitParam(name = "price", dataType = "string", value = "菜品价格", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "desc", dataType = "string", value = "菜品描述", required = false),
            @ApiImplicitParam(name = "status", dataType = "int", value = "菜品状态", required = false,defaultValue = "0"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "修改菜品成功", response = Result.class)
    })
    public Result<String> updateDish(HttpServletRequest request, HttpServletResponse response) {

        Long Id = RequestUtil.getLongParameter(request, "Id");
        String name = RequestUtil.getStringParameter(request, "name");
        String type = RequestUtil.getStringParameter(request,"type");
        String price = RequestUtil.getStringParameter(request, "price");
        String desc = RequestUtil.getStringParameter(request, "desc");
        int status = RequestUtil.getIntegerParameter(request,"status");

        Dish dish = new Dish(name, type,new BigDecimal(price), desc,status);
        dish.setId(Id);

        final Integer update = dishService.update(dish);
        if (update<=0){
            return Result.error("修改菜品失败");
        }
        return Result.success("修改菜品成功");
    }


    @Action
    @ApiOperation(value = "列出所有菜品", httpMethod = "GET", notes = "列出所有菜品")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Result.class)
    })
    public Result<List<DishDto>> listAll(HttpServletRequest request, HttpServletResponse response) {
        List<Dish> dishes = dishService.listAll();
        System.out.println("---------------------------------------------");
        System.out.println(dishes);
        if(dishes == null || dishes.size() == 0){
            return Result.success(null);
        }
        List<DishDto> list = dishes.stream().map(new Function<Dish, DishDto>() {
            @Override
            public DishDto apply(Dish dish) {
                DishDto dto = new DishDto();
                BeanUtils.copyProperties(dish, dto);
                return dto;
            }
        }).collect(Collectors.toList());
        return Result.success(list);
    }

    @Action
    @ApiOperation(value = "根据类别和状态和搜索菜品名称筛选菜品", httpMethod = "GET", notes = "根据类别和状态筛选菜品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", dataType = "string", value = "菜品类别", required = false),
            @ApiImplicitParam(name = "status", dataType = "int", value = "菜品状态", required = false),
            @ApiImplicitParam(name = "name", dataType = "string", value = "菜品名称", required = false),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Result.class)
    })
    public Result<List<DishDto>> listDishesByTypeAndStatus(HttpServletRequest request, HttpServletResponse response) {
        String type = RequestUtil.getStringParameter(request, "type");
        Integer status = RequestUtil.getIntegerParameter(request, "status");
        String name = RequestUtil.getStringParameter(request,"name");

        List<Dish> dishes = dishService.listByTypeAndStatus(type, status,name);
        if(dishes == null || dishes.size() == 0){
            return Result.success(null);
        }
        List<DishDto> list = dishes.stream().map(new Function<Dish, DishDto>() {
            @Override
            public DishDto apply(Dish dish) {
                DishDto dto = new DishDto();
                BeanUtils.copyProperties(dish, dto);
                return dto;
            }
        }).collect(Collectors.toList());
        return Result.success(list);
    }

    @Action
    @ApiOperation(value = "删除菜品", httpMethod = "POST", notes = "删除菜品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Id", dataType = "long", value = "菜品ID", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加菜品成功", response = Result.class)
    })
    public Result<String> deleteDish(HttpServletRequest request, HttpServletResponse response) {

        Long Id = RequestUtil.getLongParameter(request, "Id");

        final Integer delete = dishService.delete(Id);
        if (delete<=0){
            return Result.error("删除菜品失败");
        }
        return Result.success("删除菜品成功");
    }

    @Action
    @ApiOperation(value = "更新菜品状态", httpMethod = "POST", notes = "更新菜品状态信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Id", dataType = "long", value = "菜品ID", required = true),
            @ApiImplicitParam(name = "status", dataType = "int", value = "菜品状态", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加菜品成功", response = Result.class)
    })
    public Result<String> updateDishStatus(HttpServletRequest request, HttpServletResponse response) {

        Long Id = RequestUtil.getLongParameter(request, "Id");
        int status = RequestUtil.getIntegerParameter(request,"status");

        final Integer update = dishService.updateStatus(Id,status);
        if (update<=0){
            System.out.println(Id);
            System.out.println(status);
            return Result.error("修改菜品失败");
        }
        return Result.success("修改菜品成功");
    }

    @Action
    @ApiOperation(value = "列出所有桌位", httpMethod = "GET", notes = "列出所有桌位")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Result.class)
    })
    public Result<List<TableDto>> listAllTables(HttpServletRequest request, HttpServletResponse response) {
        List<Table> tables = tableService.listAllTables();
        if(tables == null || tables.size() == 0){
            return Result.success(null);
        }

        List<TableDto> list = tables.stream().map(new Function<Table, TableDto>() {
            @Override
            public TableDto apply(Table table) {
                TableDto dto = new TableDto();
                BeanUtils.copyProperties(table, dto);
                return dto;
            }
        }).collect(Collectors.toList());
        return Result.success(list);
    }

    @Action
    @ApiOperation(value = "获取桌位预定信息", httpMethod = "GET", notes = "获取指定桌位的预定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", dataType = "long", value = "桌位ID", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Result.class)
    })
    public Result<List<Object>> getTableReservations(HttpServletRequest request, HttpServletResponse response) {
        Long tableId = RequestUtil.getLongParameter(request, "tableId");
        List<Object> reservations = tableService.getTableReservations(tableId);
        System.out.println(reservations);
        return Result.success(reservations);
    }

    @Action
    @ApiOperation(value = "添加桌位预定", httpMethod = "POST", notes = "添加桌位预定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", dataType = "long", value = "桌位ID", required = true),
            @ApiImplicitParam(name = "dishId", dataType = "long", value = "菜品ID", required = true),
            @ApiImplicitParam(name = "quantity", dataType = "int", value = "数量", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "添加预定成功", response = Result.class)
    })
    public Result<String> addTableReservation(HttpServletRequest request, HttpServletResponse response) {
        Long tableId = RequestUtil.getLongParameter(request, "tableId");
        Long dishId = RequestUtil.getLongParameter(request, "dishId");
        Integer quantity = RequestUtil.getIntegerParameter(request, "quantity");

        boolean success = tableService.addTableReservation(tableId, dishId, quantity);
        if (success) {
            return Result.success("添加预定成功");
        } else {
            return Result.error("添加预定失败");
        }
    }

    @Action
    @ApiOperation(value = "删除桌位预定", httpMethod = "POST", notes = "删除桌位预定信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reservationId", dataType = "long", value = "预定ID", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除预定成功", response = Result.class)
    })
    public Result<String> deleteTableReservation(HttpServletRequest request, HttpServletResponse response) {
        Long reservationId = RequestUtil.getLongParameter(request, "reservationId");

        boolean success = tableService.deleteTableReservation(reservationId);
        if (success) {
            return Result.success("删除预定成功");
        } else {
            return Result.error("删除预定失败");
        }
    }

    @Action
    @ApiOperation(value = "更新桌位状态", httpMethod = "POST", notes = "更新桌位状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", dataType = "long", value = "桌位ID", required = true),
            @ApiImplicitParam(name = "status", dataType = "int", value = "桌位状态", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "更新状态成功", response = Result.class)
    })
    public Result<String> updateTableStatus(HttpServletRequest request, HttpServletResponse response) {
        Long tableId = RequestUtil.getLongParameter(request, "tableId");
        Integer status = RequestUtil.getIntegerParameter(request, "status");

        boolean success = tableService.updateTableStatus(tableId, status);
        if (success) {
            return Result.success("更新状态成功");
        } else {
            return Result.error("更新状态失败");
        }
    }

    @Action
    @ApiOperation(value = "预定桌位", httpMethod = "POST", notes = "预定桌位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", dataType = "long", value = "桌位ID", required = true),
            @ApiImplicitParam(name = "customerName", dataType = "string", value = "客户姓名", required = true),
            @ApiImplicitParam(name = "customerPhone", dataType = "string", value = "客户电话", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "预定成功", response = Result.class)
    })
    public Result<String> reserveTable(HttpServletRequest request, HttpServletResponse response) {
        Long tableId = RequestUtil.getLongParameter(request, "tableId");
        String customerName = RequestUtil.getStringParameter(request, "customerName");
        String customerPhone = RequestUtil.getStringParameter(request, "customerPhone");
        boolean success = tableService.reserveTable(tableId, customerName, customerPhone);
        if (success) {
            return Result.success("预定成功");
        } else {
            return Result.error("预定失败");
        }
    }

    @Action
    @ApiOperation(value = "结束预定", httpMethod = "POST", notes = "结束桌位预定")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", dataType = "long", value = "桌位ID", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "结束预定成功", response = Result.class)
    })
    public Result<String> endReservation(HttpServletRequest request, HttpServletResponse response) {
        Long tableId = RequestUtil.getLongParameter(request, "tableId");
        boolean success = tableService.endReservation(tableId);
        if (success) {
            return Result.success("结束预定成功");
        } else {
            return Result.error("结束预定失败");
        }
    }

    @Action
    @ApiOperation(value = "执行预定", httpMethod = "POST", notes = "执行桌位预定，对应桌位的菜品状态切换为")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableId", dataType = "long", value = "桌位ID", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "执行预定成功", response = Result.class)
    })
    public Result<String> executeReservation(HttpServletRequest request, HttpServletResponse response) {
        Long tableId = RequestUtil.getLongParameter(request, "tableId");

        boolean success = tableService.executeReservation(tableId);
        if (success) {
            return Result.success("执行预定成功");
        } else {
            return Result.error("执行预定失败");
        }
    }

    @Action
    @ApiOperation(value = "更新预定状态", httpMethod = "POST", notes = "更新预定状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "reservationId", dataType = "long", value = "预定ID", required = true),
            @ApiImplicitParam(name = "status", dataType = "int", value = "预定状态", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "更新预定状态成功", response = Result.class)
    })
    public Result<String> updateReservationStatus(HttpServletRequest request, HttpServletResponse response) {
        Long reservationId = RequestUtil.getLongParameter(request, "reservationId");
        Integer status = RequestUtil.getIntegerParameter(request, "status");

        boolean success = tableService.updateReservationStatus(reservationId, status);
        if (success) {
            return Result.success("更新预定状态成功");
        } else {
            return Result.error("更新预定状态失败");
        }
    }
}